package com.pearlsoft.webapp.core.executor;

import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.pearlsoft.webapp.core.web.util.HttpUtil;

/**
 * 
 * AbstractExecutor
 * 
 * @Creator haitingr
 * @Create-DateTime 2011-11-1
 */
public abstract class AbstractExecutor implements Executor, ApplicationContextAware {

	private ThreadLocal<HttpServletRequest> current_request = new ThreadLocal<HttpServletRequest>();

	private ThreadLocal<HttpServletResponse> current_response = new ThreadLocal<HttpServletResponse>();

	private ApplicationContext context;

	public void setApplicationContext(ApplicationContext context) throws BeansException {
		this.context = context;
	}

	/**
	 * find object from spring context
	 * 
	 * @param name
	 * @return
	 */
	protected Object getBean(String name) {
		return context.getBean(name);
	}

	/**
	 * find Object typed T from spring context
	 * 
	 * @param <T>
	 * @param name
	 * @param cls
	 * @return
	 */
	protected <T> T getBean(String name, Class<? extends T> cls) {
		return context.getBean(name, cls);
	}

	public void setHttpServletRequest(HttpServletRequest request) {
		current_request.set(request);
	}

	public void setHttpServletResponse(HttpServletResponse response) {
		current_response.set(response);
	}

	/**\
	 * 
	 * @return
	 */
	protected HttpServletRequest getHttpServletRequest() {
		return current_request.get();
	}

	/**
	 * 
	 * @return
	 */
	protected HttpServletResponse getHttpServletResponse() {
		return current_response.get();
	}

	/**
	 * 
	 * @param request
	 * @param key
	 * @return
	 */
	protected String getParameter(String key) {
		return current_request.get().getParameter(key);
	}

	/**
	 * 
	 * @param request
	 * @param key
	 * @return
	 */
	protected Integer getParameterAsInteger(String key) {
		String value = this.getParameter(key);
		return Integer.parseInt(value);
	}

	/**
	 * 
	 * @param key
	 * @param value
	 */
	protected void setAttribute(String key, Object value) {
		current_request.get().setAttribute(key, value);
	}

	/**
	 * 
	 * @param request
	 * @param values
	 */
	protected void setAttributes(Map<String, Object> values) {
		for (String key : values.keySet()) {
			current_request.get().setAttribute(key, values.get(key));
		}
	}

	/**
	 * 
	 * @param request
	 * @return
	 */
	protected HttpSession getSession() {
		return HttpUtil.getSession(current_request.get());
	}

	/**
	 * 
	 * @param request
	 * @return
	 */
	protected String getCookieValue(String name) {
		Cookie[] cookies = current_request.get().getCookies();
		for (int i = 0; i < cookies.length; i++) {
			if (cookies[i].getName().equals(name)) {
				return cookies[i].getValue();
			}
		}
		return "";
	}

	/**
	 * 
	 * @param name
	 * @param value
	 */
	protected void setCookie(String name, String value, String domain, int maxAge, String path, boolean secured) {
		Cookie cookie = new Cookie(name, value);
		cookie.setDomain(domain);
		cookie.setMaxAge(maxAge);
		cookie.setPath(path);
		cookie.setSecure(secured);
		this.current_response.get().addCookie(cookie);
	}

	/**
	 * 
	 * @param name
	 * @param value
	 * @param secured
	 */
	protected void setCookie(String name, String value, boolean secured) {
		setCookie(name, value, "", -1, "/", secured);
	}

	/**
	 * 
	 * @param name
	 * @param value
	 * @param maxAge
	 */
	protected void setCookie(String name, String value, int maxAge) {
		setCookie(name, value, "", maxAge, "/", false);
	}

	/**
	 * 
	 * @param name
	 * @param value
	 * @param domain
	 */
	protected void setCookie(String name, String value, String domain) {
		setCookie(name, value, domain, -1, "/", false);
	}

}
